package com.huilang.singleton1;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Te1 {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class clazz = Singleton.class;
        Constructor constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        Singleton singleton= (Singleton) constructor.newInstance();
        Singleton singleton1 = (Singleton) constructor.newInstance();
        System.out.println(singleton==singleton1);
    }
}
class Singleton implements Serializable {
    private static boolean flag = false;

    //私有构造方法
    private Singleton() {
        if (false == true) {
            throw new RuntimeException("不能创建多个对象");
        }
        synchronized (Singleton.class) {
            if (flag) {
                throw new RuntimeException("不能创建多个对象");
            }
            flag=true;
        }


    }


    private static class SingletonNe {
        private static final Singleton singleton=new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonNe.singleton;
    }



}